package main.java;

import main.java.fireStrategy.FireStrategy;

import java.awt.*;
import java.util.Random;

public class Tank {
    private int x, y;

    private Dir dir;
    private TankFrame tf;
    private Group group;
    private Random random = new Random();
    private boolean moving = true;
    private boolean isAlive = true;
    private static final int SPEED  = Integer.parseInt((String)PropertiesMgr.get("tankSpeed"));



    private Rectangle rect = new Rectangle();

    public static final int WIDTH = ResourceMgr.getInstance().getGoodTankD().getWidth();
    public static final int HEIGHT = ResourceMgr.getInstance().getGoodTankD().getHeight();

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public boolean getIsAlive() {
        return isAlive;
    }

    public Tank(int x, int y, Dir dir, TankFrame tf, Group group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tf = tf;
        this.group = group;
        rect.x = x;
        rect.y = y;
        rect.height = HEIGHT;
        rect.width = WIDTH;

    }

    public Rectangle getRect() {
        return rect;
    }

    public Group getGroup() {
        return group;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Dir getDir() {
        return dir;
    }

    public TankFrame getTf() {
        return tf;
    }

    public void paint(Graphics g) {
        move();
        Image image = null;
        switch (dir) {
            case LEFT:
                image = this.group == Group.GOOD? ResourceMgr.getInstance().getGoodTankL() : ResourceMgr.getInstance().getBadTankL();
                break;
            case RIGHT:
                image = this.group == Group.GOOD? ResourceMgr.getInstance().getGoodTankR() : ResourceMgr.getInstance().getBadTankR();
                break;
            case UP:
                image = this.group == Group.GOOD? ResourceMgr.getInstance().getGoodTankU() : ResourceMgr.getInstance().getBadTankU();
                break;
            case DOWN:
                image = this.group == Group.GOOD? ResourceMgr.getInstance().getGoodTankD() : ResourceMgr.getInstance().getBadTankD();
                break;
        }
        g.drawImage(image, x, y,null);
    }

    private void move() {
        if(!moving) return;
        switch (dir) {
            case UP:
                y -= SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
            case LEFT:
                x -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
        }

        // 地方坦克随机开火和转换方向
        if(this.group == Group.BAD && random.nextInt(100) > 95){
            String fs = (String) PropertiesMgr.get("badFs");
            try{
                this.fire((FireStrategy) Class.forName(fs).getMethod("getInstance").invoke(Class.forName(fs)));
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            randomDir();
        }
        boundsCheck();
        rect.x = x;
        rect.y = y;
    }

    private void boundsCheck() {
        if(this.x < 2) x = 2;
        if(this.y < 30) y = 30;
        if(this.x > TankFrame.GAME_WIDTH - Tank.WIDTH) x = TankFrame.GAME_WIDTH - Tank.WIDTH;
        if(this.y > TankFrame.GAME_HEIGHT - Tank.HEIGHT) y = TankFrame.GAME_HEIGHT - Tank.HEIGHT;

    }

    private void randomDir() {
        this.dir = Dir.values()[random.nextInt(4)];
    }

    public void fire(FireStrategy f) {
        f.fire(this);
    }

    public void die() {
        this.isAlive = false;
    }
}
